import { Injectable } from '@nestjs/common';
import { UsersEntity } from './users.entity';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';

@Injectable()
export class UsersService {
  constructor(
    @InjectRepository(UsersEntity)
    private readonly usersRepository: Repository<UsersEntity>,
  ) {}

  async findOne(username: string): Promise<UsersEntity> {
    return this.usersRepository
      .createQueryBuilder('user')
      .where('user.username = :username', { username: username })
      .addSelect('user.password')
      .leftJoinAndSelect('user.role', 'role')
      .leftJoinAndSelect('role.authority', 'authority')
      .getOne();
  }

  async save(Users) {
    let cUsers = new UsersEntity();
    cUsers = {
      ...cUsers,
      ...Users,
      role: '2', //默认普通用户
    };

    console.log(cUsers);
    await this.usersRepository.save(cUsers);
    return {
      data: null,
      code: 200,
      msg: '成功了',
    };
  }
}
